using UnityEngine;
using QFramework;
using UnityEngine.UI;
using UnityEditor.Rendering.LookDev;
using System.Linq;

namespace AsterroidX
{
	public partial class UISelectPowerUp : ViewController
	{

		private GeneratePowerUpSystem mGeneratePowerUpSysyem;


        private PowerUpUnlockSystem mPowerUpUnlockSysyem;

        private void Awake()
        {
            mGeneratePowerUpSysyem = this.GetSystem<GeneratePowerUpSystem>();

            mPowerUpUnlockSysyem=this.GetSystem<PowerUpUnlockSystem>();
        }

        private void OnEnable()
        {
            this.LocalScale(0);
            ActionKit.Sequence()
                .Lerp01(0.2f,scale=>
                {
                    this.LocalScale(scale);
                }).StartCurrentScene();

            ActionKit.Lerp(1, 0.2f, 0.3f, volumeScale =>
            {
                
                  AudioKit.MusicPlayer.VolumeScale(volumeScale);
                
            }).StartCurrentScene()
            .IgnoreTimeScale();

            this.SendCommand(new SetPauseCmd(true));

            var copyPowerUps = mPowerUpUnlockSysyem.UnlockedPowerUps.Concat(mPowerUpUnlockSysyem.PowerUpsToUnlock).ToList();

            var powerUp1Name = copyPowerUps.GetAndRemoveRandomItem();
            var powerUp2Name = copyPowerUps.GetAndRemoveRandomItem();
            var powerUp3Name = copyPowerUps.GetAndRemoveRandomItem();


            BtnCard1.transform.Find("Icon").GetComponent<Image>().sprite
                = GameObject.Find("Game").transform.Find( powerUp1Name).GetComponent<SpriteRenderer>().sprite;

            BtnCard2.transform.Find("Icon").GetComponent<Image>().sprite
                = GameObject.Find("Game").transform.Find(powerUp2Name).GetComponent<SpriteRenderer>().sprite;

            BtnCard3.transform.Find("Icon").GetComponent<Image>().sprite
                = GameObject.Find("Game").transform.Find(powerUp3Name).GetComponent<SpriteRenderer>().sprite;

            var powerUp1New = !mPowerUpUnlockSysyem.UnlockedPowerUps.Contains(powerUp1Name);
            var powerUp2New = !mPowerUpUnlockSysyem.UnlockedPowerUps.Contains(powerUp2Name);
            var powerUp3New = !mPowerUpUnlockSysyem.UnlockedPowerUps.Contains(powerUp3Name);

            if (powerUp1New)            
                BtnCard1.transform.Find("New").Show();
            else
                BtnCard1.transform.Find("New").Hide();

            if (powerUp2New)
                BtnCard2.transform.Find("New").Show();
            else
                BtnCard2.transform.Find("New").Hide();


            if (powerUp3New)
                BtnCard3.transform.Find("New").Show();
            else
                BtnCard3.transform.Find("New").Hide();



            RemoveBtnOnListeners();

            BtnCard1.Show();
            BtnCard2.Show();
            BtnCard3.Show();


            BtnCard1.onClick.AddListener(() =>
            {
                BtnCard1.Hide();
                BtnOnCLick(powerUp1New, powerUp1Name);
            });

            BtnCard2.onClick.AddListener(() =>
            {
                BtnCard2.Hide();
                BtnOnCLick(powerUp2New, powerUp2Name);
            });

            BtnCard3.onClick.AddListener(() =>
            {

                BtnCard3.Hide();
                BtnOnCLick(powerUp3New, powerUp3Name);
               
            });
            UpateUnlockedPowerUpsView();


        }
        private void OnDisable()
        {
            this.SendCommand(new SetPauseCmd(false));


            ActionKit.Lerp(0.2f, 1f, 0.3f, volumeScale =>
            {

                AudioKit.MusicPlayer.VolumeScale(volumeScale);

            }).StartCurrentScene();
        }

        private void HidePanel(string powerUpName,Transform targetTrans,bool isNew)
        {
            ActionKit.Sequence()
                .Delay(isNew?0.5f:0.2f)
                .Lerp(1, 0, 0.2f, (scale) =>
                {
                    this.LocalScale(scale);
                })
                .Callback(() =>
                {
                    mGeneratePowerUpSysyem.GeneratePowerUpByName(powerUpName, targetTrans);
                    this.Hide();

                })
                .StartCurrentScene()
                .IgnoreTimeScale();
        }

        private void UpateUnlockedPowerUpsView()
        {
            UnlockedPowerUpsRoot.DestroyChildren();

            foreach (var unlockedPowerUp in mPowerUpUnlockSysyem.UnlockedPowerUps)
            {
                var powerUp = GameObject.Find("Game").transform.Find(unlockedPowerUp).GetComponent<PowerUp>();

                UnlockedPowerUpIcon.InstantiateWithParent(UnlockedPowerUpsRoot)
                    .Self(self =>
                    {
                        self.sprite = powerUp.GetComponent<SpriteRenderer>().sprite;
                    }).Show();


            }
        }

        void RemoveBtnOnListeners()
        {
            BtnCard1.onClick.RemoveAllListeners();

            BtnCard2.onClick.RemoveAllListeners();

            BtnCard3.onClick.RemoveAllListeners();
        }

        private void BtnOnCLick(bool IsNew,string powerUpName)
        {
            RemoveBtnOnListeners();
            if (IsNew)
            {
                mPowerUpUnlockSysyem.UnlockPowerUp(powerUpName);
                UpateUnlockedPowerUpsView();
            }

            AudioKit.PlaySound("resources://sfx_click");


            HidePanel(powerUpName, FindAnyObjectByType<Player>().transform, IsNew);
        }
    }
}
